/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { Shape } from 'bizcharts';

/**
 *
 * @description 注册自定义图形
 * @param {*} type
 */
export function registerShape(type) {
  let opts = null;
  switch (type) {
    case 'round-rect':
      opts = {
        getPoints(cfg) {
          const x = cfg.x;
          let y = cfg.y;
          let y0 = cfg.y0;
          if (y0 > y) {
            const tmp = y0;
            y0 = y;
            y = tmp;
          }
          const width = cfg.size;

          return [
            { x: x - width / 2, y: y },
            { x: x - width / 2, y: y0 },
            { x: x + width / 2, y: y0 },
            { x: x + width / 2, y: y },
          ];
        },
        draw(cfg, container) {
          const points = this.parsePoints(cfg.points); // 将0-1空间的坐标转换为画布坐标
          let width;
          let height;
          let dir = 'column';
          if (points[1].x === points[2].x) {
            dir = 'row';
            width = Math.abs(points[0].x - points[1].x);
            height = Math.abs(points[1].y - points[2].y);
          } else {
            width = Math.abs(points[2].x - points[1].x);
            height = Math.abs(points[2].y - points[0].y);
          }
          const defaultRadius = Math.min(width, height) / 2;
          const radius = Math.min(defaultRadius, cfg.style.radius || 9999);
          const { fill, ...others } = cfg.style;
          const polygon = container.addShape('path', {
            attrs: {
              path:
                dir === 'row'
                  ? [
                      ['M', points[0].x, points[0].y - radius],
                      ['A', radius, radius, 0, 0, 1, points[0].x - radius, points[0].y],
                      ['L', points[1].x + radius, points[1].y],
                      ['L', points[1].x, points[1].y], // 直线
                      // ["A", radius, radius, 0, 0, 1, points[1].x, points[1].y - radius],圆角
                      ['L', points[2].x, points[2].y + radius],
                      ['L', points[2].x, points[2].y], // 直线
                      // ["A", radius, radius, 0, 0, 1, points[2].x + radius, points[2].y],圆角
                      ['L', points[3].x - radius, points[3].y],
                      ['A', radius, radius, 0, 0, 1, points[3].x, points[3].y + radius],
                    ]
                  : [
                      ['M', points[0].x + radius, points[0].y],
                      ['A', radius, radius, 0, 0, 0, points[0].x, points[0].y + radius],
                      ['L', points[1].x, points[1].y - radius],
                      ['L', points[1].x, points[1].y], // 直线
                      // ["A", radius, radius, 0, 0, 0, points[1].x + radius, points[1].y],圆角
                      ['L', points[2].x - radius, points[2].y],
                      ['L', points[2].x, points[2].y], // 直线
                      // ["A", radius, radius, 0, 0, 0, points[2].x, points[2].y - radius],圆角
                      ['L', points[3].x, points[3].y + radius],
                      ['A', radius, radius, 0, 0, 0, points[3].x - radius, points[3].y],
                    ],
              fill: cfg.color || fill,
              ...others,
            },
          });
          return polygon; // 将自定义Shape返回
        },
      };
      break;

    default:
      break;
  }
  opts && Shape.registerShape('interval', type, opts);
}

/**
 *
 * @description 修正南海诸岛
 */
export function fixNanHai(features, adcode) {
  if (adcode == '0') {
    const geoCoord = [126, 25];
    const points$1 = [
      [
        [0, 3.5],
        [7, 11.2],
        [15, 11.9],
        [30, 7],
        [42, 0.7],
        [52, 0.7],
        [56, 7.7],
        [59, 0.7],
        [64, 0.7],
        [64, 0],
        [5, 0],
        [0, 3.5],
      ],
      [[13, 16.1], [19, 14.7], [16, 21.7], [11, 23.1], [13, 16.1]],
      [[12, 32.2], [14, 38.5], [15, 38.5], [13, 32.2], [12, 32.2]],
      [[16, 47.6], [12, 53.2], [13, 53.2], [18, 47.6], [16, 47.6]],
      [[6, 64.4], [8, 70], [9, 70], [8, 64.4], [6, 64.4]],
      [[23, 82.6], [29, 79.8], [30, 79.8], [25, 82.6], [23, 82.6]],
      [[37, 70.7], [43, 62.3], [44, 62.3], [39, 70.7], [37, 70.7]],
      [[48, 51.1], [51, 45.5], [53, 45.5], [50, 51.1], [48, 51.1]],
      [[51, 35], [51, 28.7], [53, 28.7], [53, 35], [51, 35]],
      [[52, 22.4], [55, 17.5], [56, 17.5], [53, 22.4], [52, 22.4]],
      [[58, 12.6], [62, 7], [63, 7], [60, 12.6], [58, 12.6]],
      [
        [0, 3.5],
        [0, 93.1],
        [64, 93.1],
        [64, 0],
        [63, 0],
        [63, 92.4],
        [1, 92.4],
        [1, 3.5],
        [0, 3.5],
      ],
    ];

    for (let i$1 = 0, len = points$1.length; i$1 < len; i$1++) {
      for (let k = 0, len = points$1[i$1].length; k < len; k++) {
        points$1[i$1][k][0] /= 10.5;
        points$1[i$1][k][1] /= -10.5 / 0.75;

        points$1[i$1][k][0] += geoCoord[0];
        points$1[i$1][k][1] += geoCoord[1];
      }
    }

    features.push({
      type: 'Feature',
      properties: {
        id: '',
        name: '南海诸岛',
        center: geoCoord,
      },
      geometry: {
        type: 'Polygon',
        coordinates: points$1,
      },
    });
  }
  return features;
}

export const lineStyleRow = { fill: 'l(0) 0:#4d99ef 0.5:#73d3eb 1:#73d3eb' };
export const lineStyleColumn = { fill: 'l(90) 0:#73d3eb 0.5:#73d3eb 1:#4d99ef' };
export const gradualTextColumn = 'l(90) 0:#7abefe 1:#3a75ef';
export const axisLabel = {
  textStyle: {
    fill: gradualTextColumn, // 文本的颜色
    fontSize: '14', // 文本大小
    fontWeight: 'bold', // 文本粗细
  },
};

/**
 * 字体自适应
 *  */
export const changeScale = (subDom, boxDom) => {
  const boxWidth = boxDom && boxDom.offsetWidth;
  const subWidth = subDom && subDom.offsetWidth;
  if (subWidth > boxWidth) {
    const scaleRate = boxWidth / subWidth;
    subDom && (subDom.style.transform = `scale(${scaleRate})`);
  } else {
    subDom && (subDom.style.transform = 'scale(1)');
  }
};

/**
 * 根据层级格式化名称
 *  */
export const formatName = (name, level) => {
  if (level == 2) {
    return `${name}`;
  }
  if (level == 3) {
    return `${name}`;
  }
  return name;
};

/**
 * 判断一个点是否在某个范围内
 *  */
export const isPointInPolygon = (point, pts) => {
  const N = pts.length; // pts [{lat:xxx,lng:xxx},{lat:xxx,lng:xxx}]
  const boundOrVertex = true; // 如果点位于多边形的顶点或边上，也算做点在多边形内，直接返回true
  let intersectCount = 0; // cross points count of x
  const precision = 2e-10; // 浮点类型计算时候与0比较时候的容差
  let p1;
  let p2; // neighbour bound vertices
  const p = point; // point {lat:xxx,lng:xxx}

  p1 = pts[0]; // left vertex
  for (let i = 1; i <= N; ++i) {
    // check all rays
    if (p.lat == p1.lat && p.lng == p1.lng) {
      return boundOrVertex; // p is an vertex
    }
    p2 = pts[i % N]; // right vertex
    if (p.lat < Math.min(p1.lat, p2.lat) || p.lat > Math.max(p1.lat, p2.lat)) {
      // ray is outside of our interests
      p1 = p2;
      continue; // next ray left point
    }
    if (p.lat > Math.min(p1.lat, p2.lat) && p.lat < Math.max(p1.lat, p2.lat)) {
      // ray is crossing over by the algorithm (common part of)
      if (p.lng <= Math.max(p1.lng, p2.lng)) {
        // x is before of ray
        if (p1.lat == p2.lat && p.lng >= Math.min(p1.lng, p2.lng)) {
          // overlies on a horizontal ray
          return boundOrVertex;
        }
        if (p1.lng == p2.lng) {
          // ray is vertical
          if (p1.lng == p.lng) {
            // overlies on a vertical ray
            return boundOrVertex;
          }
          // before ray
          ++intersectCount;
        } else {
          // cross point on the left side
          const xinters = ((p.lat - p1.lat) * (p2.lng - p1.lng)) / (p2.lat - p1.lat) + p1.lng; // cross point of lng
          if (Math.abs(p.lng - xinters) < precision) {
            // overlies on a ray
            return boundOrVertex;
          }
          if (p.lng < xinters) {
            // before ray
            ++intersectCount;
          }
        }
      }
    } else {
      // special case when ray is crossing through the vertex
      if (p.lat == p2.lat && p.lng <= p2.lng) {
        // p crossing over p2
        const p3 = pts[(i + 1) % N]; // next vertex
        if (p.lat >= Math.min(p1.lat, p3.lat) && p.lat <= Math.max(p1.lat, p3.lat)) {
          // p.lat lies between p1.lat & p3.lat
          ++intersectCount;
        } else {
          intersectCount += 2;
        }
      }
    }
    p1 = p2; // next ray left point
  }
  if (intersectCount % 2 == 0) {
    // 偶数在多边形外
    return false;
  }
  // 奇数在多边形内
  return true;
};

/**
 * 兼容后台返回的乡镇名称
 * @param mapName 地图中的地区名称
 * @param serveName 后端数据返回的地区名称
 *  */
export const transformAreaName = (mapName, serveName, cb) => {
  if (mapName == serveName || mapName.includes(serveName)) {
    cb();
    return;
  }
  // 需要兼容的地区，已后端返回的数据为准
  const serviceNames = ['安文镇'];
  if (serviceNames.includes(serveName)) {
    cb();
  }
};
